<?php
if (!isset($model))
    $model = new BDynamicFormModel ();

//should input variable for this view
/*
 * 1)ownerName
 * 2)ownerTable
 * 3)ownerId
 * 4)ownerAttributeTable
 * 5)ownerIndex
 */

$attributeNameField = 'media_name_' . $ownerName . '_attribute';
$attributeAliasField = 'media_alias_' . $ownerName . '_attribute';
$attributeTypeField = 'media_type_' . $ownerName . '_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$attributeView = array();

if ($ownerId > 0) {
    $owner = $ownerTable->getListById($ownerId);

    //$pollAttributeTable = new BPSheet('wePollAttribute', $poll);
    $attributeView = $ownerAttributeTable->getView();

    /**
     * below code will populate attribute for product
     */
    foreach ($attributeView as $id => $attribute) {
        $nameItem = $attribute->getItem('name');
        $typeItem = $attribute->getItem('type');
        if ($nameItem instanceof BTreeItem) {
            $name = $nameItem->getValue();
            $attributeField = 'media_' . $name . $ownerIndex;
            $model->addField($attributeField);
            //get value of attribute in product 
            $attributeItem = $owner->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            } else
                $model->$attributeField = '';
        }
        if ($typeItem instanceof BTreeItem) {
//            $type = $nameItem->getValue();
//            $attributeField = 'media_' . $type . '_poll_info';
            $type = $typeItem->getValue();
            if ($type == 'image')
                $attributeField = 'media_' . $type . $ownerIndex;
            $model->addField($attributeField);
            $attributeItem = $owner->getItem($type);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
        }
    }    
}
?>
<?php
$app = Blyn::app()->getActiveApp();
$appCommon = new BAppCommon();
?>


<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script type="text/javascript">
    $(function() {
        $('#datetimepicker1').datetimepicker({
            language: 'pt-BR'
        });
    });</script>
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $bAppCommon = new BAppCommon();
        $typeList = $bAppCommon->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addAttribute'), array(
            'htmlOptions' => array('class' => 'btn'),
            'ownerId' => $ownerId,
            'ownerTable'=>$ownerTable,
            'attributeId' => 0
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>     
        <?php
        if ($ownerId > 0) {
            foreach ($attributeView as $attribute) {
                $attributeId = $attribute->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $attribute->getItem('name');
                    $typeItem = $attribute->getItem('type');
                    $aliasItem = $attribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . $ownerIndex;
                        $attributeItem = $poll->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        } else
                            $model->$attributeField = '';
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $appCommon->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deleteAttribute'), array(
                            'htmlOptions' => array('class' => 'btn'),
                            'ownerId' => $ownerId,
                            'ownerTable'=>$ownerTable,
                            'attributeId' => $attributeId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>

